/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thienloc.omap.controller.soccer;

import com.thienloc.omap.controller.AbstractCrudBean;
import com.thienloc.omap.entity.KeoChauA;
import com.thienloc.omap.entity.TranDau;
import com.thienloc.omap.session.TranDauFacade;
import com.thienloc.omap.util.JsfUtil;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ActionEvent;

/**
 *
 * @author duongnv
 */
@ManagedBean
@ViewScoped
public class LiveSoccerResult extends AbstractCrudBean {

    @EJB
    private TranDauFacade facade;
    private List<TranDau> liveMatches;
    private List<TranDau> commingMatches;
    private List<TranDau> playedMatches;

    public List<TranDau> getCommingMatches() {
        return commingMatches;
    }

    public void setCommingMatches(List<TranDau> commingMatches) {
        this.commingMatches = commingMatches;
    }

    public List<TranDau> getLiveMatches() {
        return liveMatches;
    }

    public void setLiveMatches(List<TranDau> liveMatches) {
        this.liveMatches = liveMatches;
    }

    public List<TranDau> getPlayedMatches() {
        return playedMatches;
    }

    public void setPlayedMatches(List<TranDau> playedMatches) {
        this.playedMatches = playedMatches;
    }

    public void test() {
        liveMatches = getLiveMatch1();
    }

    public void updateCommingMatch() {
        commingMatches = getCommingMatch();
    }

    public void updatePlayedMatch() {
        playedMatches = getPlayedMatch1();
    }

    public void update2PlayedMatch(ActionEvent actionEvent) {
        int id = Integer.parseInt(JsfUtil.getRequestParameter("id"));
        facade.update2PlayedMatch(id);
        liveMatches = getLiveMatch1();
        commingMatches = getCommingMatch();
        playedMatches = getPlayedMatch1();
    }

    @Override
    protected Logger getLogger() {
        return LoggerFactory.getLogger(LiveSoccerResult.class);
    }

    @Override
    protected void resetToCreateFormAction() throws Exception {
        liveMatches = getLiveMatch1();
        commingMatches = getCommingMatch();
        playedMatches = getPlayedMatch1();
    }

    @Override
    protected void validateForm() throws Exception {
    }

    @Override
    protected void findSelected(int id) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected void createSelected() throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected void editSelected() throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected void removeSelected() throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected void prepareViewAction() throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected void prepareEditAction() throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    private List<TranDau> getCommingMatch() {
        List<TranDau> result = facade.findCommingMatches();
        for (TranDau item : result) {
            List<KeoChauA> l = item.getKeoChauAList();
            if (l.isEmpty()) {
                result.remove(item);
            }
        }
        result = sortByNameOfGame(result);
        return result;
    }

    private List<TranDau> getPlayedMatch1() {
        List<TranDau> result = facade.findPlayedMatches();
        result = sortByNameOfGame(result);
        return result;
    }

    private List<TranDau> getLiveMatch1() {
        List<TranDau> result = facade.findLiveMatches();
        return result;
    }

    private List<TranDau> sortByNameOfGame(List<TranDau> list) {
        List<TranDau> result = list;
        if (result.isEmpty()) {
            return list;
        }
        for (int i = 0; i < result.size(); i++) {
            for (int j = i + 1; j < result.size(); j++) {
                String game = result.get(i).getIdGiaiDau().getTenGiaiDau();
                String g = result.get(j).getIdGiaiDau().getTenGiaiDau();
                if (g.compareToIgnoreCase(game) < 0) {
                    TranDau tmp = result.get(i);
                    result.set(i, result.get(j));
                    result.set(j, tmp);
                }
            }
        }
        return result;
    }
}
